@echo off
setlocal enabledelayedexpansion

echo ======================================
echo CTP API Visual Studio Build Tool
echo ======================================

echo Detecting Visual Studio installations...

REM Function to detect Visual Studio installations
set "VS_FOUND="
set "VS_GENERATOR="
set "VS_VCVARS="

REM Check for Visual Studio 2022 Professional (C drive)
if exist "%ProgramFiles%\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=%ProgramFiles%\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2022 Professional (D drive)
if exist "D:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=D:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2022 Community (C drive)
if exist "%ProgramFiles%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=%ProgramFiles%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2022 Community (D drive)
if exist "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2022 Build Tools (C drive)
if exist "%ProgramFiles%\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=%ProgramFiles%\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2022 Build Tools (D drive)
if exist "D:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2022"
    set "VS_GENERATOR=Visual Studio 17 2022"
    set "VS_VCVARS=D:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Professional (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Professional (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Community (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Community (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Build Tools (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2019 Build Tools (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2019"
    set "VS_GENERATOR=Visual Studio 16 2019"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Professional (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Professional (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Community (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Community (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Build Tools (C drive)
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for Visual Studio 2017 Build Tools (D drive)
if exist "D:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    set "VS_FOUND=2017"
    set "VS_GENERATOR=Visual Studio 15 2017"
    set "VS_VCVARS=D:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    goto :found_vs
)

REM Check for cl.exe directly in PATH (fallback)
where cl >nul 2>nul
if %errorlevel% equ 0 (
    echo Found cl.exe in PATH
    goto :check_compiler
)

REM No Visual Studio found
echo ERROR: No Visual Studio installation found
echo.
echo Searched locations:
echo - %ProgramFiles%\Microsoft Visual Studio\2022\Professional\
echo - D:\Program Files\Microsoft Visual Studio\2022\Professional\
echo - %ProgramFiles%\Microsoft Visual Studio\2022\Community\
echo - D:\Program Files\Microsoft Visual Studio\2022\Community\
echo - %ProgramFiles%\Microsoft Visual Studio\2022\BuildTools\
echo - D:\Program Files\Microsoft Visual Studio\2022\BuildTools\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2019\BuildTools\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\
echo - %ProgramFiles(x86)%\Microsoft Visual Studio\2017\BuildTools\
echo - D:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\
echo.
echo Please install Visual Studio 2022, 2019, or 2017
echo Professional, Community, or Build Tools editions supported
echo.
echo You can download Visual Studio Build Tools for free from:
echo https://visualstudio.microsoft.com/downloads/
pause
exit /b 1

:found_vs
echo Found Visual Studio %VS_FOUND%
echo Using generator: %VS_GENERATOR%
echo.

REM Setup Visual Studio environment
echo Setting up Visual Studio environment...
call "%VS_VCVARS%"
if %errorlevel% neq 0 (
    echo ERROR: Failed to setup Visual Studio environment
    pause
    exit /b 1
)

:check_compiler

REM Create build directory
if not exist "build" (
    echo Creating build directory...
    mkdir build
    if %errorlevel% neq 0 (
        echo ERROR: Failed to create build directory
        pause
        exit /b 1
    )
)

cd build

REM Check for CMake
where cmake >nul 2>nul
if %errorlevel% neq 0 (
    echo ERROR: CMake not found
    echo Please install CMake and add to system PATH
    pause
    exit /b 1
)

echo.
echo Generating Visual Studio project...
echo.

REM Set compiler flags based on compile_simple.py settings
set "CMAKE_CXX_FLAGS=/bigobj /EHsc /source-charset:gbk /execution-charset:gbk /W4 /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /std:c++11"

if defined VS_GENERATOR (
    cmake .. -G "%VS_GENERATOR%" -A x64 -DCMAKE_CXX_FLAGS="%CMAKE_CXX_FLAGS%"
) else (
    REM Fallback to auto-detect if VS environment is set
    cmake .. -A x64 -DCMAKE_CXX_FLAGS="%CMAKE_CXX_FLAGS%"
)
if %errorlevel% neq 0 (
    echo ERROR: CMake generation failed
    echo Trying fallback generator...
    cmake .. -DCMAKE_CXX_FLAGS="%CMAKE_CXX_FLAGS%"
    if %errorlevel% neq 0 (
        pause
        exit /b 1
    )
)

echo.
echo Visual Studio project generated successfully!
echo.

REM Build the project
echo Starting build...
echo.

cmake --build . --config Release
if %errorlevel% neq 0 (
    echo.
    echo Build failed!
    echo Trying Debug configuration...
    cmake --build . --config Debug
    if %errorlevel% neq 0 (
        echo.
        echo Build failed!
        echo Please check error messages and fix issues
        pause
        exit /b 1
    )
)

echo.
echo ======================================
echo Build completed successfully!
echo ======================================
echo.
echo Generated files:
echo Python Extension Modules:
for /r %%f in (*.pyd) do echo   %%~nxf
echo.
echo Dynamic Libraries:
for /r %%f in (*.dll) do (
    if not "%%~xf"==".pyd" echo   %%~nxf
)
echo.
echo Build complete! Press any key to exit...
pause >nul